//
//  JUChatListVC.m
//  findFriend
//
//  Created by ju on 14-9-11.
//  Copyright (c) 2014年 dono. All rights reserved.
//  聊天列表

#import "JUChatListVC.h"
#import "JUChatRoomVC.h"
#import "ChatGroup.h"

@interface JUChatListVC ()<UITableViewDataSource,UITableViewDelegate,UIAlertViewDelegate,AVSessionDelegate,AVSignatureDelegate>
{
    UITableView *_tableView;
    AVSession *_session;
    
    NSArray *_groups;
}

@end

@implementation JUChatListVC

- (void)loadView
{
    [super loadView];
    self.title = @"群组列表";
    _tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    self.view = _tableView;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setupRightBtn];
//    [self setupSession];
}

- (void)viewWillAppear:(BOOL)animated
{
    [self loadGroupsFromDB];
}

#pragma mark 从数据库加载组信息
- (void)loadGroupsFromDB
{
    _groups = [ChatGroup queryBySQL:
               [NSString stringWithFormat:@"where username='%@'",[AVUser currentUser].username]];
    [_tableView reloadData];
}

#pragma mark 初始化新建组按钮
- (void)setupRightBtn
{
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addGroup:)];
}

- (void)addGroup:(UIBarButtonItem *)item
{
    UIAlertView *alertView = [[UIAlertView alloc] init];
    alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
    alertView.title = @"请输入组名";
    alertView.delegate = self;
    [alertView addButtonWithTitle:@"取消"];
    [alertView addButtonWithTitle:@"确定"];
    [alertView show];
}

#pragma mark UIAlertView代理
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    //点击确定
    if (buttonIndex == 1) {
        UITextField *textField = [alertView textFieldAtIndex:0];
        if (textField) {
            NSString *groupName = textField.text;
            ChatGroup *g = [[ChatGroup alloc] init];
            g.groupName = groupName;
            g.username = [AVUser currentUser].username;
            [g save];
            JUChatRoomVC *room = [[JUChatRoomVC alloc] init];
            room.chatGroup = g;
//            room.session = _session;
            room.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:room animated:YES];
        }
    }
}

#pragma mark tableview 数据源和代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _groups.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"chatCell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"chatCell"];
    }
    ChatGroup *g = _groups[indexPath.row];
    cell.textLabel.text = g.groupName;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ChatGroup *g = _groups[indexPath.row];
    JUChatRoomVC *room = [[JUChatRoomVC alloc] init];
    room.chatGroup = g;
//    room.session = _session;
    room.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:room animated:YES];
}

#pragma mark 初始化session
- (void)setupSession
{
    _session = [[AVSession alloc] init];
    _session.sessionDelegate = self;
    _session.signatureDelegate = self;
    [_session open:[AVUser currentUser].username withPeerIds:nil];
}

#pragma mark AVSession代理
- (void)onSessionOpen:(AVSession *)session
{
    NSLog(@"session open");
}

- (void)onSessionPaused:(AVSession *)session
{
    NSLog(@"session paused");
}

- (void)onSessionResumed:(AVSession *)seesion
{
}

- (void)onSessionMessage:(AVSession *)session message:(NSString *)message peerId:(NSString *)peerId
{
    
}

- (void)onSessionMessageSent:(AVSession *)session message:(NSString *)message toPeerIds:(NSArray *)peerIds
{
    
}

- (void)onSessionMessageFailure:(AVSession *)session message:(NSString *)message toPeerIds:(NSArray *)peerIds
{
    
}

- (void)onSessionStatusOnline:(AVSession *)session peers:(NSArray *)peerIds
{
    
}

- (void)onSessionStatusOffline:(AVSession *)session peers:(NSArray *)peerId
{
    
}

- (void)onSessionError:(AVSession *)session withException:(NSException *)exception
{
    NSLog(@"%@",exception);
}

@end
